package com.cc8w.handler;

import org.apache.ibatis.executor.result.ResultMapException;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.StringTypeHandler;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *如果mybatis查出来字段为null,给默认值“”
 */
@MappedJdbcTypes(JdbcType.VARCHAR) //这个注解在一开始是没有加的。如果不加需要在mapper.xml中使用typeHandler="" 进行引用
public class MyStringTypeHandler extends StringTypeHandler {

    @Override
    public String getResult(ResultSet rs, String columnName) throws SQLException {
        String result;
        try {
            result = getNullableResult(rs, columnName);
        } catch (Exception e) {
            throw new ResultMapException("Error attempting to get column '" + columnName + "' from result set.  Cause: " + e, e);
        }
        return result;

    }
    @Override
    public String getNullableResult(ResultSet rs, String columnName)
            throws SQLException {
        String rsString = rs.getString(columnName);
        if(rsString==null){
            return "";
        }
        return rsString;
    }

    @Override
    public String getNullableResult(ResultSet rs, int columnIndex)
            throws SQLException {
        String rsString = rs.getString(columnIndex);
        if(rsString==null){
            return "";
        }
        return rsString;
    }

    @Override
    public String getNullableResult(CallableStatement cs, int columnIndex)
            throws SQLException {
        String rsString = cs.getString(columnIndex);
        if(rsString==null){
            return "";
        }
        return rsString;
    }
}